<?php

namespace App\Models;

use App\Models\Posts\WxPost;
use App\Models\Posts\WxUserCircle;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class WxCircle extends Model
{
	use HasDateTimeFormatter;
    use SoftDeletes;

    protected $table = 'wx_circle';


    public function wxPlate()
    {
        return $this->hasOne(WxPlate::class,'id','plate_id');
    }

    public function wxUser()
    {
        return $this->hasOne(WxUser::class,'id','user_id');
    }

    public function wxPosts(){

        return $this->hasMany(WxPost::class,'circle_id','id');
    }

    /**
     * 限制显示2条
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function posts(){
        return $this->hasMany(WxPost::class,'circle_id','id')->select()->limit(2);
    }

    public function wxUserCircle()
    {
        return $this->hasMany(WxUserCircle::class,'circle_id','id');
    }

    /**
     * 圈子的用户信息
     */
    public function getCircleUserAttribute()
    {
        return WxUser::where('id',$this->user_id)->first(['user_avatar','weixin_name']);
    }

    /**
     * 圈子的帖子数量
     */
    public function getCirclePostsCountAttribute()
    {
        return WxPost::where('circle_id',$this->id)->count();
    }


    /**
     * 圈子的关注人数
     */
    public function getCircleFollowCountAttribute()
    {
        return WxUserCircle::where('circle_id',$this->id)->count();
    }

}
